<?php

namespace app\api\controller;

use app\admin\model\activity\Slider;
use app\admin\model\goods\Mycategory;
use app\common\controller\Api;
use think\Db;
use think\File;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * @Explain :获取首页自定义品类
     * @Date : 2020/9/1 10:29
     * @Author : 河南邦耀网络科技有限公司
     */
    public function getIndexCategory(){
        $data=(new Mycategory())->select();
        if(empty($data)) $this->error('暂无数据');
        $i=1;
        foreach ($data as $row){
            if($row['type']==1){
                $result['indexCate'][]=$row;
            }elseif($row['type']==2){
                $result['recommend'][]=$row;
            }elseif($row['type']==3){
                $result['category'][]=$row;
            }else{
                $result['mc'][]=$row;
            }
        }
        $this->success('ok',$result);
    }

    /**
     * @Explain :获取首页自定义品类
     * @Date : 2020/9/1 10:29
     * @Author : 河南邦耀网络科技有限公司
     */
    public function getCate(){
        $data=(new Mycategory())->where(['type'=>1])->order('order desc')->select();
        if(empty($data)) $this->error('暂无数据');
        $i=1;
        foreach ($data as $row) {
            $cate[] = $row;
            if ($i % 2 == 0) {
                $result[] = $cate;
                $cate = [];
            }
            $i++;
        }
        if(!empty($cate)){
            $result[]=$cate;
        }
        $this->success('ok',$result);
    }

    /**
     * @Explain :轮播图
     * @Date : 2020/9/9 11:50
     * @Author : 河南邦耀网络科技有限公司
     */

    public function getSliderImg(){
        $result=(new Slider())->where(['type'=>1])->select();
        if($result){
            $this->success('ok',$result);
        }else{
            $this->success('暂无轮播图');
        }
    }

    /**
     * @Explain :引导图
     * @Date : 2020/9/9 11:50
     * @Author : 河南邦耀网络科技有限公司
     */

    public function getStartImg(){
        $result=(new Slider())->where(['type'=>2])->select();
        if($result){
            $this->success('ok',$result);
        }else{
            $this->success('暂无轮播图');
        }
    }

    /**
     * @Explain : 首页瀑布流
     * @Date : 2020/9/1 15:00
     * @Author : 河南邦耀网络科技有限公司
     */
    public function getPinterest(){
        $pn=$this->request->request('page',1);
        if($pn<=10) {
            $content = $this->redis->get('pinterest');
            if (empty($content)) {
                $data = (new \app\admin\model\goods\Goods())->field(['goodsid', 'title', 'image', 'sales', 'price','discount'])
                    ->where(['status' => 1])->where('atIndex', '=', 1)->order('weight desc')->limit(100)->select();

                $this->redis->set('pinterest', json_encode($data, JSON_UNESCAPED_UNICODE));
                $this->redis->expire('pinterest', 7200);
            } else {
                $data = json_decode($content, true);
            }
            $start = ($pn - 1) * 10;
            $data = array_slice($data, $start, 10);
        }else {
            $data = (new \app\admin\model\goods\Goods())->field(['goodsid', 'title', 'image', 'sales', 'price','discount'])
                ->where(['status' => 1])->where('atIndex', '=', 1)->order('weight desc')->page($pn, 10)->select();
        }
        foreach ($data as &$row){
            $row['spellPrice']=bcmul($row['price'],$row['discount']*0.01,2);
        }
        if($data){
            $this->success('ok',$data);
        }else{
            $this->success('暂无商品');
        }
    }



    /**获取拼单信息
     * @Explain :
     * @Date : 2020/9/11 11:46
     * @Author : 河南邦耀网络科技有限公司
     */
    public function getSpellOrder(){
        $orderM=new \app\admin\model\order\Order();
        $orders=$orderM->field('uid,totalPrice,price')->where(['isFinished'=>1])->limit(20)->order('finishTime desc')->select();
        foreach ($orders as &$row){
            $row['userInfo']=Db::table('pt_user')->field('nickname,avatar')->where(['id'=>$row['uid']])->find();
        }
        $this->success('ok',$orders);
    }

    /**
     * @Explain :
     * @Date : 2020/9/18 9:52
     * @Author : 河南邦耀网络科技有限公司
     */
    public function cartNum(){
        $num=Db::table('pt_cart')->where(['uid'=>$this->auth->id,'status'=>1])->count();
        $this->success('ok',$num);
    }

}
